function aptRobotSelectionInitBC2() {

    // Variable declaration	
    var selectorId = "apt_RobotSelection_Id_BC2";
    var robots = sessionStorage.getItem("apt_Common.AvailableRobotsWithProcess");
    var lastSelectedRobot = sessionStorage.getItem("apt_AT_LastSelectedRobot");
    var advisedId = emosWS.getNewElementId();

    $('.diahead_left').children('.processname').text(lastSelectedRobot);

    robots = robots.split('|');
    var handlerObj = [];

    var toshow = sessionStorage.getItem("apt_Common.BC2");
    toshow = toshow.split('|');

    // Get Robots with same Process as the current one
    var currentProcess = sessionStorage.getItem("apt_" + lastSelectedRobot + ".Process") + sessionStorage.getItem("apt_" + lastSelectedRobot + ".ProcessDetails");

    var robotsWithSameProcess = [];
    robots.forEach(function (rob) {
        if (rob !== lastSelectedRobot) {
            if (currentProcess === sessionStorage.getItem("apt_" + rob + ".Process") + sessionStorage.getItem("apt_" + rob + ".ProcessDetails")) {
                robotsWithSameProcess.push(rob);
            }
        }
    });

    function createDom() {
        var robotPositions = {};

        var min = [];
        var max = [];

        toshow.forEach(function (robName) {
            var highlighted = "";
            if (robName === lastSelectedRobot) {
                highlighted = "enabled";
            }
            var positionx = parseInt(sessionStorage.getItem("apt_" + robName + ".PositionX"));
            var positiony = parseInt(sessionStorage.getItem("apt_" + robName + ".PositionY"));

            robotPositions[robName] = {
                x: positionx,
                y: positiony,
                selected: highlighted
            };

            if (min.length === 0) min = [positionx, positiony];
            if (max.length === 0) max = [positionx, positiony];

            if (positionx <= min[0]) min[0] = positionx;
            else if (positionx >= max[0]) max[0] = positionx;

            if (positiony <= min[1]) min[1] = positiony;
            else if (positiony >= max[1]) max[1] = positiony;
        });

        createDynamicTable(min, max, selectorId, "robotButtonTable");

        for (var rob in robotPositions) {
            //var robOpc = sessionStorage.getItem("apt_" + rob + ".OpcTopic");
			var PLCOpc = sessionStorage.getItem("apt_PLC.OpcTopic");
            var innerId = getElementGlobalID();
            var html = "<div class='robSelection'>";
            html += "<div class='emosbutton aptStandardButton " + robotPositions[rob].selected + " robotselectionbuttonBC2' id = 'robotButton_" + rob + "'><div id = '" + innerId + "' class='innerImageDiv'>" + rob + "</div></div>";

            $("#" + innerId).text(rob);
            //var processStateTag = sessionStorage.getItem("apt_" + rob + ".Tags.ProcessState");
            var robnr = sessionStorage.getItem("apt_" + rob + ".RobNr");
//            var processStateTag = ".IO_Hmi.BG[" + robnr + "].Common.Out.iState";
            var processStateTag = sessionStorage.getItem("apt_PLC.Tags.BG" + robnr + ".Out.State");
            handlerObj.push({
                tag: PLCOpc + processStateTag,
                id: "robotButton_" + rob,
                function: function(tag, id) { handleButtonState(tag, id); }
            });
            html += "</div>";

            var x = robotPositions[rob].x;
            var y = robotPositions[rob].y;
            placeHtmlInTable(html, x, y, selectorId);
        }

        handlerObj.forEach(function(obj) {
            obj.function(obj.tag, obj.id);
        });

        $('#apt_ProcessControlButtons').removeClass("hide");
    }

    function handleButtonState(tag, id) {
        emosWS.advise(tag, function(msg) {
            var element = $('#' + id);
            switch (msg.value) {
                case "2":
                    handleButtonStateCss(element, "green");
                    break;
                case "4":
                    handleButtonStateCss(element, "red");
                    break;
                case "8":
                    handleButtonStateCss(element, "orange");
                    break;
                default:
                    handleButtonStateCss(element, "grey");
            }
        }).setId(advisedId);
    }

    function handleButtonStateCss(element, addedColor) {
        var cssClasses = $(element).attr('class').split(/\s+/);
        for (var i = 0; i < cssClasses.length; i++) {
            if (cssClasses[i].startsWith("state")) {
                element.removeClass(cssClasses[i]);
            }
        }
        element.addClass("state" + addedColor);
    }

    function eventHandler() {
        $('.robotselectionbuttonBC2').click(function (e) {
            $(this).closest('.diagnose').removeClass('aptClass');
            $('.diahead_left').children('.processname').text('...');
            var rob = e.currentTarget.innerText;
            sessionStorage.setItem("apt_AT_LastSelectedRobot", rob);
            // Remove active of all buttons
            $(".robotselectionbuttonBC2").removeClass("enabled");
            $(e.currentTarget).addClass("enabled");
            console.log("Button pressed " + rob);
 
            var appname = sessionStorage.getItem("apt_appName");

            var robotProcess = sessionStorage.getItem("apt_" + rob + ".Process");
            var robotProcessDetails = sessionStorage.getItem("apt_" + rob + ".ProcessDetails");

            var processToDisplay = sessionStorage.getItem("apt_AT_" + robotProcess + '.' + robotProcessDetails);
            var processSchemePath = processToDisplay.split("/")[0];
            sessionStorage.setItem("apt_currentProccesSchemePath", processSchemePath);

            var frame = document.getElementById("plants");
            var url = frame.src;

            url = url.split(processSchemePath + "/")[0] + processToDisplay + "?app=" + appname + '&rob=' + rob + '&cache=' + (new Date()).getTime();

            // Check if we can access the frame obj (especially for IE necessary)
            if (frame !== null) {
                if (frame.src) {
                    frame.src = url;
                }
                else if (frame.contentWindow !== null && frame.contentWindow.location !== null) {
                    frame.contentWindow.location = url;
                }
                else {
                    frame.setAttribute('src', url);
                }
            } 
        });
    }

    createDom();
    eventHandler();

}
